var config = require('./config');

var express = require('express');

//
var bodyParser = require('body-parser');
var errorhandler = require('errorhandler');
var session = require('express-session');
var cookieParser = require('cookie-parser');

var bunyan = require('bunyan');

var auth = require('./middlewares/auth');

//# routers
//
var backendRouter = require('./routers/backend_router');

var app = express();

//# body
//
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

//# session and cookie
//
app.use(cookieParser(config.session_backend));

app.use(session({
    secret: config.session_backend,
    resave: false,
    saveUninitialized: true
}));

//# the app views config
//
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.engine('html', require('ejs-mate'));

//# static file
//
app.use(express.static('public'));

//# 验证
//
// app.use(auth.managerRequired);
app.use(auth.authManager);

//# routers
app.use('/backend', backendRouter);

if (app.get('env') === 'development') {
    app.use(errorhandler());
}

app.listen(config.port, function() {
    console.log('Server listening on http://localhost:' + config.port + ', Ctrl+C to stop')
})